package manage.commons.tags;

import java.util.List;

import manage.modules.dept.model.Dept;
import manage.modules.group.model.Group;
import manage.modules.role.model.Role;
import manage.modules.worker.service.WorkerService;

import org.apache.commons.lang3.StringUtils;

/**
 * 员工帮助标签，查询员工信息时异步加载信息
 * 
 * @author dongji
 * @since 2012-9-6 下午03:50:19
 */
public class WorkerTag extends AbstractTag {

	private static final long serialVersionUID = -4459796109014389209L;

	private String type;
	private long workerId;
	private final static String BLANK = "";
	private final static char COMMA_SYMBOL = ',';
	private static WorkerService workerService;

	@Override
	protected String doTag() {
		String returnStr = BLANK;
		if (StringUtils.equals("dept", type)) {
			returnStr = selectDeptStrByWorkerId(workerId);
		} else if (StringUtils.equals("group", type)) {
			returnStr = selectGroupStrByWorkerId(workerId);
		} else if (StringUtils.equals("role", type)) {
			returnStr = selectRoleStrByWorkerId(workerId);
		}
		return returnStr;
	}

	/**
	 * 得到员工所属部门的字符串
	 * 
	 * @param workerId
	 * @return
	 */
	public String selectDeptStrByWorkerId(long workerId) {
		List<Dept> depts = workerService.listWorkerOfDept(workerId);
		StringBuilder detpNameStr = new StringBuilder(1024);
		for (Dept dept : depts) {
			detpNameStr.append(dept.getDeptName() + COMMA_SYMBOL);
		}
		if (StringUtils.isNotBlank(detpNameStr)) {
			return detpNameStr.toString().substring(0, detpNameStr.length() - 1);
		}
		return BLANK;
	}

	/**
	 * 得到员工所属组别的字符串
	 * 
	 * @param workerId
	 * @return
	 */
	public String selectGroupStrByWorkerId(long workerId) {
		List<Group> groups = workerService.listWorkerOfGroup(workerId);
		StringBuilder groupNameStr = new StringBuilder(1024);
		for (Group group : groups) {
			groupNameStr.append(group.getGroupName() + COMMA_SYMBOL);
		}
		if (StringUtils.isNotBlank(groupNameStr)) {
			return groupNameStr.toString().substring(0, groupNameStr.length() - 1);
		}
		return BLANK;
	}

	/**
	 * 得到员工所属角色的字符串
	 * 
	 * @param workerId
	 * @return
	 */
	public String selectRoleStrByWorkerId(long workerId) {
		List<Role> roles = workerService.listWorkerOfRole(workerId);
		StringBuilder roleNameStr = new StringBuilder(1024);
		for (Role role : roles) {
			roleNameStr.append(role.getRoleName() + COMMA_SYMBOL);
		}
		if (StringUtils.isNotBlank(roleNameStr)) {
			return roleNameStr.toString().substring(0, roleNameStr.length() - 1);
		}
		return BLANK;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setWorkerId(long workerId) {
		this.workerId = workerId;
	}

	public static void setWorkerService(WorkerService workerService) {
		WorkerTag.workerService = workerService;
	}

}
